package com.qianying.controller.admin;

import com.qianying.comment.CommonResult;
import com.qianying.service.FileUploadService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * 文件上传控制器
 * 仅负责文件上传，返回访问路径
 */
@RestController
@RequestMapping("/admin/fileUpload")
@Tag(name = "文件上传管理", description = "通用文件上传接口")
public class FileUploadController {

    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传文件（仅上传，不关联业务）
     */
    @PostMapping(value = "/upload", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
    @Operation(summary = "上传文件", description = "仅上传文件并返回访问路径",
            parameters = {
            @Parameter(name = "file", description = "文件保存路径", required = true)
    })
    public CommonResult<Map<String, String>> upload(@RequestParam("file") MultipartFile file) {
        return fileUploadService.uploadFile(file);
    }

    /**
     * 删除文件
     */
    @PostMapping("/delete")
    @Operation(summary = "删除文件", description = "根据文件路径删除服务器上的文件")
    public CommonResult<?> delete(
            @Parameter(description = "文件路径", required = true)
            @RequestParam String filePath) {
        return fileUploadService.deleteFile(filePath);
    }
}